﻿/// <reference path="../../Ext/ext-all-debug.js" />
/// <reference path="../../MyJS/MyStyle.js" />
/// <reference path="../../MyJS/MyWebsite.js" />
/// <reference path="../../MyJS/Basic.js" />
/// <reference path="../Interface.js" />
Ext.onReady(function () {
    MyJS.MainFrame.createFramework();
});

MyJS.extend(MyJS.MainFrame, {
    //************************************下面这些方法是扩展MyJS.MainFrame对象的。
    createFramework: function () {
        try {
            //Ext.QuickTips.init();
            //Ext.state.Manager.setProvider(Ext.create('Ext.state.CookieProvider'));

            //马上创建一个Ext对象（顶部部分）
            var oNorth = Ext.create('Ext.Component', {
                region: 'north',
                height: 32,
                autoEl: {
                    tag: 'div',
                    html: '管理系统'
                }
            });
            //延迟创建对象（页面底部）
            var oSouth = {
                xtype: "panel", //如果xtype是panel，可以不指定，默认就是panel
                region: 'south',
                contentEl: 'south',
                split: true,
                height: 100,
                minSize: 100,
                maxSize: 200,
                collapsible: true,
                collapsed: true,
                title: 'South',
                margins: '0 0 0 0'
            };
            //页面主题部分
            var oEast = {
                xtype: 'tabpanel',
                region: 'east',
                title: 'East Side',
                dockedItems: [{
                    dock: 'top',
                    xtype: 'toolbar',
                    items: ['->', {
                        xtype: 'button',
                        text: 'test',
                        tooltip: 'Test Button'
                    }]
                }],
                animCollapse: true,
                collapsible: true,
                split: true,
                width: 225, // give east and west regions a width
                minSize: 175,
                maxSize: 400,
                margins: '0 5 0 0',
                activeTab: 1,
                tabPosition: 'bottom',
                items: [{
                    html: '<p>A TabPanel component can be a region.</p>',
                    title: 'A Tab',
                    autoScroll: true
                }, Ext.create('Ext.grid.PropertyGrid', {
                    title: 'Property Grid',
                    closable: true,
                    source: {
                        "(name)": "Properties Grid",
                        "grouping": false,
                        "autoFitColumns": true,
                        "productionQuality": false,
                        "created": Ext.Date.parse('10/15/2006', 'm/d/Y'),
                        "tested": false,
                        "version": 0.01,
                        "borderWidth": 1
                    }
                })]
            };
            //页面左边菜单大纲部分
            var oWest = {
                region: 'west',
                stateId: 'navigation-panel',
                id: 'west-panel', // see Ext.getCmp() below
                title: '菜单大纲',
                split: true,
                width: 120,
                minWidth: 120,
                maxWidth: 200,
                collapsible: true,
                animCollapse: true,
                margins: '0 auto',
                layout: 'accordion',
                xtype: 'panel',
                items: this.getLeftMenuItems()
            };
            var oCenter = Ext.create('Ext.tab.Panel', {
                id: 'mainPanel',
                region: 'center', // a center region is ALWAYS required for border layout
                deferredRender: false,
                activeTab: 0,     // first tab initially active
                items: [{
                    title: '系统主页',
                    closable: false,
                    autoScroll: true
                }]
            });

            //创建主框架页面。
            var viewport = Ext.create('Ext.Viewport', {
                layout: 'border',
                items: [oNorth, oWest, oCenter]
            });
        } catch (e) {
            alert(e.message);
        }
    },
    openPage: function (sUrl, sTitle, bLoadInIframe) {
        /// <summary>打开一个新的页面</summary>
        /// <param name="sUrl" type="String">页面的Url路径</param>
        /// <param name="bLoadInIframe" type="Boolean">采用autoLoad加载的模式，还是采用iframe加载页面的模式</param>
        if (!sUrl) return;
        var oGetTab = this.getPageByTitle(sTitle);
        if (oGetTab) {
            oGetTab.show();
        }
        else {
            var oTabPanel = Ext.getCmp("mainPanel");
            sUrl = MyWebsite.parseToUrl(sUrl);
            var oConfig;
            if (bLoadInIframe) {
                oConfig = {
                    html: '<iframe scrolling="auto" frameborder="0" width="100%" height="100%" src="' + sUrl + '" />',
                    title: sTitle
                }
            }
            else {
                oConfig = {
                    autoLoad: { url: sUrl, scripts: true, nocache: true },
                    title: sTitle
                }
            }
            oTabPanel.add(oConfig).show();
        }
    },
    getPageByTitle: function (sTitle) {
        /// <summary>根据Url获取打开的tab</summary>   
        var oTabPanel = Ext.getCmp("mainPanel");
        for (var i = 0, j = oTabPanel.items.length; i < j; i++) {
            var oTab = oTabPanel.items.getAt(i);
            if (oTab.title === sTitle) return oTab;
        }
        return null;
    },

    //************************************下面这些方法定义给内部调用的
    callMenuBtnFun: function (sParams) {
        /// <summary>模块按钮的单击事件</summary>
        if (sParams) {
            //如果是Url的话，则直接打开一个tab页面
            if (sParams.startsWith("url:")) {
            }
            else if (sParams.startsWith("js:")) {  //执行一个jsFunction
                var sFun = sParams.rightOf("js:");
                if (sFun) {
                    eval(sFun);
                }
            }
        }
    },
    getLeftMenuItems: function () {
        /// <summary>获取左边菜单栏</summary>
        //[{ "ID": 2, "ParentID": 0, "Title": "个人设置", "ClickFunction": "", "PowerCode": 0, "OrderIndex": -1, "Status": 1, "IconUrl": "", "ChildModules": [{ "ID": 3, "ParentID": 2, "Title": "密码安全", "ClickFunction": "", "PowerCode": 0, "OrderIndex": -1, "Status": 1, "IconUrl": "", "ChildModules": []}] }, { "ID": 4, "ParentID": 0, "Title": "系统设置", "ClickFunction": "", "PowerCode": 0, "OrderIndex": -1, "Status": 1, "IconUrl": "", "ChildModules": []}]
        var aNewMenus = new Array();
        var sLeftMenuJSON = MyRemoteCall.execBLLFunctionSync("ModuleHelper.GetCurUserCanAccessModulesJSON");
        if (sLeftMenuJSON) {
            var aMenus = eval(sLeftMenuJSON);
            for (var i = 0; i < aMenus.length; i++) {
                var aChildMenus = aMenus[i].ChildModules;
                var aChildMenuBtns = new Array();
                if (aChildMenus && aChildMenus.length > 0) {
                    for (var j = 0; j < aChildMenus.length; j++) {
                        aChildMenuBtns.push({
                            xtype: 'button',
                            iconAlign: 'top',
                            height: 80,
                            iconCls: 'buttonImage',
                            clickFunction: aChildMenus[j].ClickFunction,
                            icon: MyWebsite.parseToUrl(aChildMenus[j].IconUrl),
                            handler: function () {
                                MyJS.MainFrame.callMenuBtnFun(this.clickFunction);
                            },
                            text: '<span><br><br><br>' + aChildMenus[j].Title + '</span>'
                        });
                    }
                }
                aNewMenus.push({ title: aMenus[i].Title,
                    layout: {
                        type: 'vbox',
                        align: 'stretch'
                    },
                    overflowY: 'auto',
                    icon: MyWebsite.parseToUrl(aMenus[i].IconUrl),
                    items: aChildMenuBtns
                });
            }
        }
        return aNewMenus;
    }
}, true);
